package com.example.wanghao.wangyixinwen.view;

import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by wanghao on 2016/5/16.
 */
public class DepthPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;

    public void transformPage(View view, float position) {

//        view 有2个不同对象
//        当前页与下一页
//
//        第一个view对象的position [-0,-1] = [0,-1]
//        第二个view对象的position[1,0]

//        System.out.println("view :"+ view);
//        System.out.println("position :"+ position);

        int pageWidth = view.getWidth();//屏幕的宽度
        System.out.println("pageWidth :" +pageWidth);

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);//设置透明度为不可见

        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
//            第一个view对象的position [-0,-1] = [0,-1]
            //A页面
            view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);

        } else if (position <= 1) { // (0,1]
            //        第二个view对象的position[1,0]
            //B页面
            //[0,1]
            view.setAlpha(1 - position);
            //[-480,0]
            //根据position的值的改变实时向左移动b页面，为了让他永远显示在屏幕的正中间
            view.setTranslationX(pageWidth * -position);

            // Scale the page down (between MIN_SCALE and 1)

            //0.75+0.25*0= 0.75起始值
            //0,75+0.25*1=1 结束值
            //[0.75,1]
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}
